package com.banyuan.dao;

import com.banyuan.domain.User;
import com.banyuan.utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {

    // 从数据库里查处所有的记录
    public static ResultSet selectAll() throws SQLException {
        ResultSet rs = JDBCUtils.executeQuery("select * from users");
        return rs;
    }


    // 从数据库里增加一个用户
    public static int insert(User user) throws SQLException {
        int i = JDBCUtils.executeUpdate("insert into users(name,age,birthday,sex) VALUES(?,?,?,?)",
                user.getName(), user.getAge(), user.getBirthday(), user.getSex());
        return i;
    }

    // 从数据库里删除一个用户

    public static int delete(Integer id) throws SQLException {
        int i = JDBCUtils.executeUpdate("delete from users where id=?", id);
        return i;
    }

    // 根据id查询一条记录
    public static ResultSet selectUser(Integer id) throws SQLException {
        ResultSet rs = JDBCUtils.executeQuery("select * from users where id = ?", id);
        return rs;
    }

    // 从数据库里修改一条数据
    public static int update(User user) throws SQLException {
        int i = JDBCUtils.executeUpdate("update users set name=?,age=?,birthday=?,sex=? where id=?",
                user.getName(), user.getAge(), user.getBirthday(), user.getSex(), user.getId());
        return i;

    }



}




